package com.burgeon.backend.EnterpriseWeChat.rest.vip;

import com.burgeon.backend.EnterpriseWeChat.entity.Vip;
import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class FindNewVipByEmpId extends RESTBase {
    private final static String RANGE = "20";//分页数量
    private String ip;
    private String account;
    private String password;

    public FindNewVipByEmpId(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public ArrayList<Vip> getData(String empId, String creatIonDate, String endTime, String storeId, int page) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(empId,creatIonDate,endTime,storeId,page), url);
//        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(new JSONArray(str).getJSONObject(0).getString("message"));
        }

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<Vip> vipList = new ArrayList<>();

        if(jsonArray.length()>0){
            for (int i = 0; i < jsonArray.length(); i++) {
                Vip vip = new Vip();
                vip.setVipName(jsonArray.getJSONArray(i).getString(0));
                vip.setCardNo(jsonArray.getJSONArray(i).getString(1));
                if("null".equals(jsonArray.getJSONArray(i).getString(2))){
                    vip.setLogoImg("http://lfwd.cloudsmicro.com:8080/img/EnterpriseWeChat/vip/app.png");
                }else {
                    vip.setLogoImg(jsonArray.getJSONArray(i).getString(2));
                }
                vip.setOpenid(jsonArray.getJSONArray(i).getString(3));
                vip.setAmount(jsonArray.getJSONArray(i).getString(4));
                vip.setIntegral(jsonArray.getJSONArray(i).getString(5));
                vip.setId(jsonArray.getJSONArray(i).getString(6));
                vip.setTotAmtActual(jsonArray.getJSONArray(i).getString(7));
                vip.setLastDate(jsonArray.getJSONArray(i).getString(8));
                vip.setLastAmtActual(jsonArray.getJSONArray(i).getString(9));
                vip.setPhone(jsonArray.getJSONArray(i).getString(10));
                vip.setExternalUserId(jsonArray.getJSONArray(i).getString(11));
                vipList.add(vip);
            }


        }
        System.err.println(vipList.size());
        return vipList;
    }

    private MultiValueMap Data(String empId,String creatIonDate,String endTime,String storeId,int page) {
        HashMap params = new HashMap();
        ArrayList colums = new ArrayList();//查询字段

        colums.add("VIPNAME");//vip姓名
        colums.add("CARDNO");//卡号
        colums.add("LOGO_URL");//头像
//        colums.add("YC_8LABELDEFINE_ID");//累计购物金额
        colums.add("OPENID2");
        colums.add("AMOUNT");//储值余额
        colums.add("INTEGRAL");//积分
        colums.add("ID");
        colums.add("'FA_VIPACC_ID;TOT_AMT_ACTUAL'");//	累计购物金额
        colums.add("'FA_VIPACC_ID;LASTDATE'");//最近购物日
        colums.add("'FA_VIPACC_ID;LAST_AMT_ACTUAL'");//最近购物金额
        colums.add("MOBIL");
        colums.add("EXTERNALUSERID");//企业微信我的顾客标识

        params.put("columns", colums);
        params.put("table", "C_VIP");//表名
        params.put("range",RANGE);
//        params.put("count","true");
        int r = Integer.valueOf(RANGE);
        params.put("start",String.valueOf((page-1)*r));


        HashMap paramss = new HashMap();
        HashMap expr1 = new HashMap();
        if(empId != null && !empId.isEmpty()){
            expr1.put("column", "'HR_EMPLOYEE_ID;ID'");
            expr1.put("condition", empId);
        }else {
            expr1.put("column", "'C_STORE_ID'");
            expr1.put("condition", storeId);
        }

        HashMap expr2 = new HashMap();
        String ac =  "'"+creatIonDate+"~"+endTime+"'";
        expr2.put("column", "'CREATIONDATE'");
        expr2.put("condition", ac);
        paramss.put("combine", "and");
        paramss.put("expr1", expr1);
        paramss.put("expr2", expr2);

        params.put("params", paramss);

        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        FindNewVipByEmpId g = new FindNewVipByEmpId("39.104.73.78:5321", "nea@burgeon.com.cn", "burgeon5321");
        try {
            System.err.println(g.getData("718","202101"+"01","20210131","",1));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
